import type { ApiResponse, FileUploadResponse } from "@/types";
import { post } from "@/utils/request";



/**
 * 上传文件
 * @param file 文件对象
 * @param type 文件类型（avatar/chat_image）
 * @param onProgress 上传进度回调
 * @returns 文件上传响应
 */
export const uploadFile = async (
  file: File,
  type: "avatar" | "chat/image", 
  onProgress?: (percent: number) => void
): Promise<ApiResponse<FileUploadResponse>> => {
  const formData = new FormData();
  formData.append("file", file);
  formData.append("type", type);

  return post(`/files/${type}`, formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total && onProgress) {
        const percent = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
        onProgress(percent);
      }
    },
  })
};

/**
 * 删除文件
 * @param objectName 文件对象名称
 */
export const deleteFile = async (objectName: string): Promise<ApiResponse<void>> => {
  return post(`/files/${objectName}`);
};
